package cpa.prowon.gui.actions;

import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.SelectionChangedEvent;

import pho.prowon.category.VisibleIndexCategory;
import cpa.prowon.gui.MainWindow;
import cpa.prowon.gui.MultiImageInformationWindow;
import cpa.prowon.gui.SingleImageInformationWindow;
import cpa.prowon.image.ImageContainer;

/**
 * Opens the image information window to view and edit the IPTC information. Depending
 * on the number of selected images, either the a window for single images or multiple
 * image maintenance is opened
 * @author Chris
 */
public class ImageInformationAction
	extends PatchedAction
	implements ISelectionChangedListener {

	// Properties
	private IStructuredSelection selection;
	
	/**
	 * Constructor
	 * @param MainWindow The window where the action is used
	 * @author Chris
	 */
	public ImageInformationAction(MainWindow w) {
		super(w);
		setEnabled(false);
	}

	/**
	 * Return the title key
	 * @return The title key
	 * @author Chris
	 */
	protected String getTitleKey() {
		return "menu_item_edit_imageinformation";
	}

	/**
	 * Return the tooltip key
	 * @return The tootip key
	 * @author Chris
	 */
	protected String getTooltipKey() {
		return "";
	}

	/**
	 * Return the image key
	 * @return The image key
	 * @author Chris
	 */
	protected String getImageKey() {
		return "";
	}

	/**
	 * Run the action
	 * @author Chris
	 */
	public void run() {

		// Get the index category for a refresh later
		VisibleIndexCategory ic = (VisibleIndexCategory) window.getListPart().getTableViewer().getInput();

		// Depending on the number of selected images, open the single or multi image information window
		if (selection.size() == 1) {
			ImageContainer selected_image = (ImageContainer) selection.getFirstElement();
			SingleImageInformationWindow iw = new SingleImageInformationWindow(window, selected_image);
			iw.open(!selected_image.canBeChanged());
			ic.refreshImage(selected_image);
			window.getPreviewPart().setPictureInfo(selected_image);
		} else {
			Object[] selectionArray = selection.toArray();
			ImageContainer[] selected_images = new ImageContainer[selectionArray.length];
			for (int i=1; i <= selectionArray.length; i++) {
				selected_images[i-1] = (ImageContainer) selectionArray[i-1];
			}
			MultiImageInformationWindow iw = new MultiImageInformationWindow(window, selected_images);
			iw.open(false);
			for (int i=1; i <= selected_images.length; i++) {
				ic.refreshImage(selected_images[i-1]);
			}
		}

		// Refresh the categorization viewer
		window.refreshCategoryPart();
		
		// Refresh the picture list information
		window.refreshListPart();
		
	}

	/**
	 * Listen to the selection changed event in order to enable or disable the action
	 * @param SelectionChangedEvent The event that occurs when the selection changed
	 * @author Chris
	 */
	public void selectionChanged(SelectionChangedEvent event) {

		// Get the selection and check whether it's one image only, if not, disable the action
		selection = window.getPictureListSelection();
		if (selection.size() == 0) {
			setEnabled(false);
			return;
		}
		setEnabled(true);

	}
	
}
